import { director,Component,_decorator,Node ,JsonAsset,game,Game,sys,screen} from "cc";
import { hiyooo } from "./HiYOOO";
import {  resLoader } from "./common/loader/ResLoader";
import { GameQueryConfig } from "../module/config/GameQueryConfig";
import { GameConfig } from "../module/config/GameConfig";
import { message } from "./common/event/MessageManager";
import { LayerManager } from "./gui/layer/LayerManager";
import { StorageManager } from "./common/storage/StorageManager";
import { AudioManager } from "./common/audio/AudioManager";
import { TimerManager } from "./common/timer/TimerManager";
import { GameManager } from "./game/GameManager";
import { StorageSecuritySimple } from "./common/storage/StorageSecuritySimple";
import { DEBUG } from "cc/env";
import { HttpRequestMethods, HttpResponseCallback, HttpResponseType } from "../libs/network/HttpRequest";
import { EventMessage } from "./common/event/EventMessage";

const {property } = _decorator;
let isInited = false;
export class Root extends Component{
    @property({type:Node,tooltip:"游戏节点"})
    game:Node = null!;
    @property({type:Node,tooltip:"UI节点"})
    gui:Node = null!;

    //框架常驻节点
    private persistRoot:Node = null!;

    protected onLoad(): void {
        if (!isInited) {
            isInited = true;    //防止重复初始化
            this.initStart()
            this.loadConfig().then();
        }
    }

    private async loadConfig(){
        //TODO:加载配置
        this.persistRoot = new Node("HiYOOOFrameworkPersistRoot"); // 创建常驻节点
        director.addPersistRootNode(this.persistRoot); // 添加常驻节点
        //资源管理模块 
        hiyooo.res = resLoader;
        let config_name = "config_proudction";

        if (DEBUG) {
            config_name ="config_development"
        }
        const config  = await hiyooo.res.loadAsync(config_name,JsonAsset);
  
        if (config) {
            //游戏网页版配置文件
           hiyooo.config.query = new GameQueryConfig();
           // 游戏配置文件
           hiyooo.config.game = new GameConfig(config);
            //资源管理模块初始化
           hiyooo.res.defaultBundleName = hiyooo.config.game.bundleDefault;
           hiyooo.res.init(hiyooo.config.game.date.bundle);
           //本地存储模块
           hiyooo.storage = new StorageManager();
           hiyooo.storage.init( new StorageSecuritySimple());

            //全局通知模块
           hiyooo.message = message;
            //音频管理模块 
           hiyooo.audio = this.persistRoot.addComponent(AudioManager);
           hiyooo.audio.load();
            //定时器管理模块
           hiyooo.timer = this.persistRoot.addComponent(TimerManager);
            //游戏管理模块
           hiyooo.game = new GameManager(this.game);

           //游戏管理模块gui
           hiyooo.gui = new LayerManager();
           hiyooo.gui.mobileSafeArea = hiyooo.config.game.mobileSafeArea;
           //@ts-ignore
           hiyooo.gui.initLayer(this.gui);

           //http请求模块
           hiyooo.http.server = hiyooo.config.game.httpServer;
           hiyooo.http.timeout = hiyooo.config.game.httpTimeout;

           game.frameRate = hiyooo.config.game.frameRate;
           this.enabled = true;
           this.init();
           this.run();
           //释放资源
           hiyooo.res.release(config_name);
        }else{
            this.loadConfig().then()
        }
    }

    

    /**
     * 框架开始初始化，此时还未进行配置加载
     */
    protected initStart():void{

    }
    /**
     * 初始化页面 ， 配置已加载完成
     */
    protected initGui():void{

    }
    /**
     * 初始化业务模块
     */
    protected initEcsSystem(){

    }

    /**
     * 加载完配置文件执行
     */
    protected run():void{

    }

    protected update(dt: number): void {
        hiyooo.ecs.excute(dt);
    }

    /**
     * 配置文件加载完成之后，进行的初始化工作
     */
    private init():void{

        this.initGui();
        this.initEcsSystem();
        
        hiyooo.ecs.init();

        game.on(Game.EVENT_SHOW,this.onShow,this);
        game.on(Game.EVENT_HIDE,this.onHide,this);
        if (!sys.isMobile) {
            screen.on("window-resize",()=>{
                hiyooo.message.dispathchEvent(EventMessage.GAME_RESIZE);
            },this)
            screen.on("fullscreen-change",()=>{
                hiyooo.message.dispathchEvent(EventMessage.GAME_FULL_SCREEN);
            },this)
        }
        screen.on("orientation-change",()=>{
            hiyooo.message.dispathchEvent(EventMessage.GAME_ORIENTATION);
        },this)
    }



    private onShow():void{
        hiyooo.timer.load();
        hiyooo.audio.resumeAll();
        director.resume();
        game.resume();
        hiyooo.message.dispathchEvent(EventMessage.GAME_SHOW)
    }

    private onHide():void{
        hiyooo.timer.save();
        hiyooo.audio.pauseAll();
        director.pause();
        game.pause();
        hiyooo.message.dispathchEvent(EventMessage.GAME_HIDE);
    }
}

